﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TCDC_System.Register
{
    public partial class Register1 : System.Web.UI.Page
    {
        private string lang { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lang = Request.QueryString["lang"];
                
                LoadDDLType(lang);
                LoadDDLGroup(lang);
                LoadDDLCampaign(lang,"","");
            }
        }

        private void LoadDDLType(string lang)
        {
            try
            {
                string host = HttpContext.Current.Request.Url.Authority;

                DataSet ds = new DataSet();
                ds.ReadXml("http://" + host + "/Attribute/xml/MemberType.ashx");

                dlType.DataSource = ds;
                string DataText = (lang == "en") ? "type_name_en" : "type_name_th";

                dlType.DataTextField = DataText;
                dlType.DataValueField = "type_id";
                dlType.DataBind();

                string DefaultVal = (lang == "en") ? "-- Select --" : "-- เลือกรายการ --";
                dlType.Items.Insert(0, new ListItem(DefaultVal, ""));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadDDLGroup(string lang)
        {
            try
            {
                string host = HttpContext.Current.Request.Url.Authority;

                DataSet ds = new DataSet();
                ds.ReadXml("http://" + host + "/Attribute/xml/CampaignGroup.ashx");

                dlGroup.DataSource = ds;
                string DataText = (lang == "en")? "group_name_en" : "group_name_th";

                dlGroup.DataTextField = DataText;
                dlGroup.DataValueField = "group_id";
                dlGroup.DataBind();

                string DefaultVal = (lang == "en") ? "-- Select --" : "-- เลือกรายการ --";
                dlGroup.Items.Insert(0, new ListItem(DefaultVal, ""));

            }

            catch (Exception ex)
            {

                throw ex;

            }
        }

        private void LoadDDLCampaign(string lang, string type, string group)
        {
            try
            {
                string host = HttpContext.Current.Request.Url.Authority;

                DataSet ds = new DataSet();
                ds.ReadXml("http://" + host + "/Attribute/xml/Campaign.ashx?type=" + type + "&group=" + group);

                dlCampaign.DataSource = ds;
                string DataText = (lang == "en") ? "campaign_name_en" : "campaign_name_th";

                dlCampaign.DataTextField = DataText;
                dlCampaign.DataValueField = "campaign_id";
                dlCampaign.DataBind();

                string DefaultVal = (lang == "en") ? "-- Select --" : "-- เลือกรายการ --";
                dlCampaign.Items.Insert(0, new ListItem(DefaultVal, ""));

            }

            catch (Exception ex)
            {

                throw ex;

            }
        }

        protected void dlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = dlType.Text;
            string group = dlGroup.Text;

            LoadDDLCampaign(this.lang, type, group);
        }

        protected void dlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = dlType.Text;
            string group = dlGroup.Text;

            LoadDDLCampaign(this.lang, type, group);

        }

        protected void txtNext_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register_Page2.aspx");
        }
    }
}